package org.dydl.service.system.impl;

import org.apache.commons.lang.StringUtils;
import org.dydl.common.dto.ResponseData;
import org.dydl.common.dto.ResponseMessage;
import org.dydl.dao.system.repository.ISysUserRepository;
import org.dydl.model.system.SysUser;
import org.dydl.service.system.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service("sysUserService")
public class SysUserServiceImpl implements ISysUserService {

	@Autowired
	private ISysUserRepository sysUserRepository;

	@Override
	public ResponseMessage getUserList(int page, int rows, String name) {
		Pageable pageable = new PageRequest(page - 1, rows);
		Page<SysUser> pageList = null;
		if (StringUtils.isNotBlank(name)) {
			pageList = sysUserRepository.getSysUserListLike(String.format("%s%s%s", "%", StringUtils.trim(name), "%"),
					pageable);
		} else {
			pageList = sysUserRepository.getSysUserList(pageable);
		}
		return new ResponseMessage(true, new ResponseData<SysUser>(page, pageList.getTotalPages(),
				((int) pageList.getTotalElements()), pageList.getContent()));
	}

	@Override
	public ResponseMessage save(SysUser sysUser) {
		return new ResponseMessage(true, sysUserRepository.save(sysUser));
	}

	@Override
	public ResponseMessage delete(long ids) {
		sysUserRepository.delete(ids);
		return new ResponseMessage(true);
	}
}
